//
//  HSYSelectBankViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/20.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "HSYSelectBankViewController.h"
#import "UIImage+Tools.h"
#import "HSYGetBankListRequest.h"
#import "HSYBankListModel.h"
#import "HSYSelectBankBranchCell.h"

static NSString *cellReuseIdentifier = @"cellReuseIdentifier";

@interface HSYSelectBankViewController ()

@property (nonatomic, strong) HSYBankListModel *listModel;

@property (nonatomic, weak) IBOutlet UIView *searchContentView;
@property (nonatomic, weak) IBOutlet UITextField *searchTextField;
@property (nonatomic, weak) IBOutlet UIButton *searchButton;

@property (nonatomic, strong) HSYGetBankListRequest *task;


@end

@implementation HSYSelectBankViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor hsyDefaultBackgroudColor];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.tableFooterView = [UIView new];

    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    [self.tableView registerNib:[UINib nibWithNibName:@"HSYSelectBankBranchCell" bundle:nil] forCellReuseIdentifier:cellReuseIdentifier];
    
    self.tableView.estimatedRowHeight = 100;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    [self hsy_headerBeginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}

- (void)setupNavigationBarBackgroundImage{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(1, 1)]  forBarMetrics:UIBarMetricsDefault];
    [self.leftAreaLBtn setImage:[UIImage imageNamed:@"left"]  forState:UIControlStateNormal];
    [self.titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.titleButton setTitle:@"选择开户行" forState:UIControlStateNormal];
}

- (UIEdgeInsets)hsy_tableViewEdgeInsets{
    return UIEdgeInsetsMake(70, 0, 0, 0);
}


- (BOOL)hsy_allowPullDown{
    return YES;
}

- (BOOL)hsy_allowPullUp{
    return YES;
}

- (void)hsy_loadServerData{
    [super hsy_loadServerData];
    
    if (self.pageIdex == 0) { //刷新数据，请求第一页
        self.pageIdex= self.pageIdex + 1;
    }
    
    NSString *keyword = [self.searchTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    HSYGetBankListRequest *task = [[HSYGetBankListRequest alloc] init];
    task.currentPage = self.pageIdex;
    task.keyword = keyword.length ? keyword : nil;
    
    self.task = task;
    
    //    [BQActivityView showActiviTy];
    __weak typeof(self) wSelf = self;
    
    [HSYNetworkManager POST:task completionHander:^(id responseObject, NSString *resultString) {
        //        [BQActivityView hideActiviTy];
        
        if (responseObject) {
            HSYBankListModel *model = [HSYBankListModel mj_objectWithKeyValues:responseObject];
            
            if (wSelf.pageIdex != 1) { //为1是下拉刷新
                [wSelf.listModel.list addObjectsFromArray:model.list];
                model.list = wSelf.listModel.list;
            }
            wSelf.listModel = model;
            
            if (model.page.totalPage <= wSelf.pageIdex) {//没有更多数据
                [wSelf hsy_loadServerDataComplete:kDataLoadCompleteStatusNoMoreData];
            }else{
                [wSelf hsy_loadServerDataComplete:kDataLoadCompleteStatusDefault];
            }
            
        }else{
            if (resultString) {
                [wSelf showMessage:resultString];
            }
            [wSelf hsy_loadServerDataComplete:kDataLoadCompleteStatusFailed];
        }
    }];
}

- (IBAction)search:(id)sender{
    [self.searchTextField endEditing:YES];
    if (self.task)
        [self.task cancelCurrentTask];
    
    [self hsy_headerBeginRefreshing];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.listModel.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HSYBankModel *model = self.listModel.list[indexPath.row];
    HSYSelectBankBranchCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier forIndexPath:indexPath];
    cell.titleLabel.attributedText = [self attributedStringFromString:model.bankName];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.selectBankBlock) {
        HSYBankModel *model = self.listModel.list[indexPath.row];
        self.selectBankBlock(model.bankName, nil);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}


//行间距28，
- (NSAttributedString *)attributedStringFromString:(NSString *)string{
    string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.lineSpacing = 12;
    
    NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc] initWithString:string];
    [str1 addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, string.length)];
    
    return str1;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
